﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using RATSharpW7.ViewModel;
using System.Windows.Navigation;
using RATSharpW7.Converters;
using RATSharpW7.ViewModel.CalendarViewModel;

namespace RATSharpW7.View
{
    /// <summary>
    /// Main calendar view
    /// </summary>
    public partial class Calendar : PhoneApplicationPage
    {
        public AbstractCalendarViewModel model { get; set; }


        public ICalendarDataProvider provider;

        public bool isNewPageInstance = false;
        public Calendar()
        {
            InitializeComponent();
            isNewPageInstance = true;
        }

        private void Cal_MonthChanging(object sender, WPControls.MonthChangedEventArgs e)
        {
            int year = e.Year;
            int month = e.Month;
            model.loadCalendarOccupation(month, year, new EventHandler<ModifyReservationEventArgs>(calendarOccupationLoadedHandler));
        }

        private void Cal_DateClicked(object sender, WPControls.SelectionChangedEventArgs e)
        {
            DayViewData preparedData = new DayViewData(e.SelectedDate, provider);
            NavigationService.Navigate("View", typeof(Day), preparedData);
        }

        private void calendarOccupationLoadedHandler(object sender, ModifyReservationEventArgs e)
        {
            if (!e.succesfull)
            {
                ViewUtils.reportError(e.message);
            }
            else
            {
                Cal.Refresh();
            }
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (isNewPageInstance)
            {
                if (model == null)
                {
                    if (State.ContainsKey("CalendarViewModel"))
                    {
                        model = State["CalendarViewModel"]
                                           as CalendarUserViewModel;
                    }
                    else
                    {
                        provider = NavigationContext.GetData<ICalendarDataProvider>();
                        model = provider.getCalendarModel();
                    }
                }
                DataContext = model;
            }
            //just refresh whole calendar (datamodel could changed recently)
            Cal.Refresh();
            model.loadCalendarOccupation(Cal.SelectedMonth, Cal.SelectedYear, new EventHandler<ModifyReservationEventArgs>(calendarOccupationLoadedHandler));
            isNewPageInstance = false;
        }


        public class DayViewData
        {
            public DateTime selectedDate { get; private set; }
            public ICalendarDataProvider dataProvider { get; private set; }

            public DayViewData(DateTime selectedDate, ICalendarDataProvider dataProvider)
            {
                this.selectedDate = selectedDate;
                this.dataProvider = dataProvider;
            }
        }
    }
}